package com.doradance.app.mine;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.doradance.R;
import com.doradance.account.UrlCenter;
import com.doradance.common.http.MyHttpRequest;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

public class FeedBackActivity extends BaseActivity {

    @Bind(R.id.et_feed_back)
    EditText mEtFeedBack;
    @Bind(R.id.et_feed_phone)
    EditText mEtFeedPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolsBar();
    }

    private void initToolsBar() {
        setTitle(getString(R.string.opinion_feedback));
        getCustomToolbar().addLeftImageButton(R.mipmap.back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_feed_back, null);
    }

    public static void start(Context activity) {
        Intent intent = new Intent(activity, FeedBackActivity.class);
        activity.startActivity(intent);
    }

    @OnClick({R.id.btn_feed_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_feed_back:
                upFeedBack();
                break;
        }
    }

    private void upFeedBack() {
        if(TextUtils.isEmpty(mEtFeedBack.getText().toString())){
            EasyToast.showToast(getApplicationContext(),"请留下您的建议");
        }else{
            // 获取packagemanager的实例
            PackageManager packageManager = getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = null;
            try {
                packInfo = packageManager.getPackageInfo(getPackageName(),0);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            MyHttpRequest request = new MyHttpRequest(this);
            JSONObject joj = new JSONObject();
            try {
                joj.put("plantform","Android");
                joj.put("version",packInfo.versionName+"");
                joj.put("content",mEtFeedBack.getText().toString());
                joj.put("contact_number",mEtFeedPhone.getText().toString());

            } catch (JSONException e) {
                e.printStackTrace();
            }
            request.post(UrlCenter.POST_FEEDBACK,joj, new DataRequestListener<String>(String.class) {
                @Override
                public void onStart(Context context, String url) {
                    super.onStart(context, url);
                }

                @Override
                public void onSuccess(String o) {
                    super.onSuccess(o);
                    EasyToast.showToast(getApplicationContext(),"提交成功");
                    finish();
                }

                @Override
                public void onFailure(Throwable t, int errorNo, String strMsg) {
                    super.onFailure(t, errorNo, strMsg);
                    EasyToast.showToast(getApplicationContext(),strMsg);
                }
            });
        }
    }
}
